﻿using PSI.Filters;
using PSI.Helper;
using PSI.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PSI.Controllers {
    public class PermissionController : PSIController {
        private UserService UserService { get { return new UserService(); } }
        private PermissionService PermissionService { get { return new PermissionService(); } }

        public ActionResult Index() {
            if (!PermissionService.HasPermission(FIdConst.PERMISSION)) {
                return Redirect("/");
            }

            ViewBag.UserName = UserService.GetLoginUserName();

            return View();
        }

        [HttpPost]
        public ActionResult GetRole() {
            var data = PermissionService.GetRole();

            return Json(data);
        }

        [HttpPost]
        public ActionResult GetPermission(string roleId) {
            var data = PermissionService.GetPermission(roleId);

            return Json(data);
        }

        [HttpPost]
        public ActionResult GetUsers(string roleId) {
            var data = PermissionService.GetUsers(roleId);

            return Json(data);
        }

        [HttpPost]
        public ActionResult SelectPermission(string idList) {
            var data = PermissionService.SelectPermission(idList);

            return Json(data);
        }

        [HttpPost]
        public ActionResult SelectUsers(string idList) {
            var data = PermissionService.SelectUsers(idList);

            return Json(data);
        }

        [HttpPost]
        public ActionResult EditRole(string id, string name, string permissionIdList, string userIdList) {
            var result = PermissionService.EditRole(id, name, permissionIdList, userIdList);

            return Json(result);
        }

        [HttpPost]
        public ActionResult DeleteRole(string id) {
            var result = PermissionService.DeleteRole(id);

            return Json(result);
        }
    }
}
